/**
 * 
 */
package com.rpg.base.interactive.flyweight;

import java.lang.reflect.Method;

import com.dalonedrau.jogl.opengl.sprite.SpriteBase;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;

/**
 * @author Donald
 */
public class InteractiveObject implements BaseInteractiveObject {
	/** flags indicating the IO is taking damage of a specific type. */
	private long dmgFlags = 0;
	private char[] name = new char[64];
	/**
	 * {@inheritDoc}
	 */
	@Override
	public char[] getLocalizedName() {
		return name;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setLocalizedName(final String val) {
		name = val.toCharArray();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setLocalizedName(final char[] val) {
		name = val;
	}
	private String[]	animationNames;
	private int[]		animations;
	/** any flags that have been set. */
	private long		ioFlags = 0;
	private int			refId;
	private Method[]	scripts	= new Method[1];
	private SpriteBase	sprite;
	/**
	 * Creates a new instance of {@link InteractiveObject}.
	 * @param id the reference id
	 */
	public InteractiveObject(final int id) {
		refId = id;
		animations = new int[0];
		animationNames = new String[0];
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void addAnimation(final String name, final int id) throws Exception {
		if (name == null) {
			PooledStringBuilder sb = StringBuilderPool.getInstance()
					.getStringBuilder();
			sb.append("ERROR! InteractiveObject.addAnimation() - ");
			sb.append("null value sent in parameters");
			Exception ex = new Exception(sb.toString());
			sb.returnToPool();
			throw ex;
		}
		int index = -1;
		for (int i = 0; i < animationNames.length; i++) {
			if (animationNames[i] == null
					|| animationNames[i] != null
							&& animationNames[i].equals(name)) {
				index = i;
				break;
			}
		}
		if (index == -1) {
			// extend the names array
			index = animations.length;
			String[] tempStr = new String[animations.length + 1];
			System.arraycopy(animationNames, 0, tempStr, 0,
					animationNames.length);
			animationNames = tempStr;
			// extend the animations array
			int[] temp = new int[animations.length + 1];
			System.arraycopy(animations, 0, temp, 0, animations.length);
			animations = temp;
		}
		animationNames[index] = name;
		animations[index] = id;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void addIOFlag(final long flag) {
		ioFlags |= flag;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void clearIOFlags() {
		ioFlags = 0;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public int getAnimation(final String name) throws Exception {
		if (name == null) {
			PooledStringBuilder sb = StringBuilderPool.getInstance()
					.getStringBuilder();
			sb.append("ERROR! InteractiveObject.getAnimation() - ");
			sb.append("null value sent in parameters");
			Exception ex = new Exception(sb.toString());
			sb.returnToPool();
			throw ex;
		}
		int index = -1;
		for (int i = 0; i < animationNames.length; i++) {
			if (animationNames[i] != null
							&& animationNames[i].equals(name)) {
				index = i;
				break;
			}
		}
		if (index == -1) {
			PooledStringBuilder sb = StringBuilderPool.getInstance()
					.getStringBuilder();
			sb.append("ERROR! InteractiveObject.getAnimation() - ");
			sb.append("no animation set for ");
			sb.append(name);
			Exception ex = new Exception(sb.toString());
			sb.returnToPool();
			throw ex;
		}
		return animations[index];
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public int getRefId() {
		return refId;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public SpriteBase getSprite() {
		return sprite;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean hasIOFlag(final long flag) {
		return (ioFlags & flag) == flag;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void removeIOFlag(final long flag) {
		ioFlags &= ~flag;
	}
	public void setMethod(final int index, final Method method) {
		if (index >= 0
				&& index < scripts.length) {
			scripts[index] = method;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setSprite(SpriteBase sb) {
		sprite = sb;
	}
}
